/**
 * Copyright(C), 2013-2014
 *		 Shenzhen Coordinates Software Development Co., Ltd.
 * com.cnhis.cloudhealth.open.service.plugin.weixin.util.HttpRequest.java
 */
package com.attilax.net;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.attilax.util.ExUtil;

/**
 * 
 * v3 s521 add post put ati
 * 
 * @Description: TODO(绫诲姛鑳芥弿杩�
 * @Package com.cnhis.cloudhealth.open.service.plugin.weixin.util
 * @ClassName HttpRequest
 * @author Administrator
 * @date 2016骞�鏈�3鏃�涓嬪崍3:31:03
 * @version 鐗堟湰鍙�V1.0.0
 */

public class HttpClientUtil4_0_1 {

	// private UrlEncodedFormEntity asList_NameValuePair(Map map) throws
	// UnsupportedEncodingException {
	// List<NameValuePair> values_ListNameValuePair = new
	// ArrayList<NameValuePair>();
	//
	// // values.add(new BasicNameValuePair("id", "1"));
	//
	// // values.add(new BasicNameValuePair("name", "xiaohong"));
	// addPostParams(map, values_ListNameValuePair);
	// UrlEncodedFormEntity urlEncodedFormEntity = new
	// UrlEncodedFormEntity(values_ListNameValuePair, HTTP.UTF_8);
	// return urlEncodedFormEntity;
	// }

	private byte[] execute(HttpClient httpCilent, HttpUriRequest HttpUriRequest1) {
		byte[] srtResult;

		HttpResponse httpResponse;
		try {
			httpResponse = httpCilent.execute(HttpUriRequest1);

			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (statusCode == 200 || statusCode == 201) {

				HttpEntity entity = httpResponse.getEntity();

				srtResult = EntityUtils.toByteArray(entity);

				return srtResult;
			} else if (statusCode == 400) {
				throw new RuntimeException("httpResponse.getStatusLine().getStatusCode() == 400");
			} else if (statusCode == 500) {
				throw new RuntimeException("httpResponse.getStatusLine().getStatusCode() == 500");
			} else
				throw new RuntimeException("httpResponse.getStatusLine().getStatusCode() == " + statusCode);

		} catch (ParseException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		} // 鑾峰緱杩斿洖鐨勭粨鏋�

	}
	
	public    static void httpget(String url,String outputstream1File) throws FileNotFoundException
	
	{
		 
		new HttpClientUtil4_0_1().	httpget(url,new FileOutputStream(new File(outputstream1File)));
		 
	}
	public void httpget(String url,OutputStream outputstream1) {

		HttpClient httpCilent = new DefaultHttpClient(); // Creates
															// CloseableHttpClient
															// instance
															// with
															// default
															// configuration.
		HttpGet httpGet = new HttpGet(url);

		// httpCilent.execute(httpGet);
		String srtResult = "";

		HttpResponse httpResponse;
		try {
			httpResponse = httpCilent.execute(httpGet);

			if (httpResponse.getStatusLine().getStatusCode() == 200) {

				httpResponse.getEntity().writeTo(outputstream1);
			//	srtResult = EntityUtils.toString(httpResponse.getEntity());

			//	return srtResult;
			} else if (httpResponse.getStatusLine().getStatusCode() == 400) {
				throw new RuntimeException("httpResponse.getStatusLine().getStatusCode() == 400");
			} else if (httpResponse.getStatusLine().getStatusCode() == 500) {
				throw new RuntimeException("httpResponse.getStatusLine().getStatusCode() == 500");
			}

		} catch (ParseException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		} // 鑾峰緱杩斿洖鐨勭粨鏋�
		 
	}
	public String httpget(String url) {

		HttpClient httpCilent = new DefaultHttpClient(); // Creates
															// CloseableHttpClient
															// instance
															// with
															// default
															// configuration.
		HttpGet httpGet = new HttpGet(url);

		// httpCilent.execute(httpGet);
		String srtResult = "";

		HttpResponse httpResponse;
		try {
			httpResponse = httpCilent.execute(httpGet);

			if (httpResponse.getStatusLine().getStatusCode() == 200) {

			 
				srtResult = EntityUtils.toString(httpResponse.getEntity());

				return srtResult;
			} else if (httpResponse.getStatusLine().getStatusCode() == 400) {
				throw new RuntimeException("httpResponse.getStatusLine().getStatusCode() == 400");
			} else if (httpResponse.getStatusLine().getStatusCode() == 500) {
				throw new RuntimeException("httpResponse.getStatusLine().getStatusCode() == 500");
			}

		} catch (ParseException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		} // 鑾峰緱杩斿洖鐨勭粨鏋�
		throw new RuntimeException(" err");
	}

	private void addPostParams(Map<String, String> map, List<NameValuePair> list) {
		Iterator iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {

			Entry<String, Object> elem = (Entry<String, Object>) iterator.next();
			list.add(new BasicNameValuePair(elem.getKey(), elem.getValue().toString()));
		}

	}

	public static void main(String[] args) {
		// Map<String, Object> recordParams = new HashMap<String, Object>();
		// // key鍙傝�HoMessageRecord鍙傛暟鍚�
		// recordParams.put("receiverName", "闆嶆鐖�);
		// recordParams.put("description", "闆嶆鐖峰緢鍕ゅ姵");
		//
		// String local = "http://localhost:8080/crmweb/cloud/hoMessageRecord";
		// String remote =
		// "http://120.25.59.85:10500/crmweb/cloud/hoMessageRecord";
		// String rtnObj = HttpClientUtil.postEntiry2RestEasy(remote,
		// recordParams);
		// System.out.println(rtnObj);
	}
}
